package com.jinyou.utils.common;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DBObjectUtil {
	/**
	* @方法名: dbObject2Bean
	* @方法描述:DBObject转换成JavaBean  
	* @方法参数 @param dbObject
	* @方法参数 @param bean
	* @方法参数 @return
	* @方法参数 @throws IllegalAccessException
	* @方法参数 @throws InvocationTargetException
	* @方法参数 @throws NoSuchMethodException   
	* @返回类型 T    
	* @throws
	 */
	public static <T> T dbObject2Bean(DBObject dbObject, T bean)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		if (bean == null) { // 测试已通过
			return null;
		}
		Field[] fields = bean.getClass().getDeclaredFields();
		for (Field field : fields) {
			String varName = field.getName();
			Object object = dbObject.get(varName);
			if (object != null) {
				BeanUtils.setProperty(bean, varName, object);
			}
		}
		return bean;
	}

	/**
	 * 
	 * @方法名: bean2DBObject
	 * @方法描述:JavaBean转DBobjetc
	 * @方法参数 @param bean
	 * @方法参数 @return
	 * @方法参数 @throws IllegalArgumentException
	 * @方法参数 @throws IllegalAccessException
	 * @返回类型 DBObject
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public static <T> DBObject bean2DBObject(T bean)
			throws IllegalArgumentException, IllegalAccessException {
		if (bean == null) {
			return null;
		}
		DBObject dbObject = new BasicDBObject();
		// 获取对象对应类中的所有属性域
		Field[] fields = bean.getClass().getDeclaredFields();
		for (Field field : fields) {
			// 获取属性名
			String varName = field.getName();
			// 修改访问控制权限
			boolean accessFlag = field.isAccessible();
			if (!accessFlag) {
				field.setAccessible(true);
			}
			Object param = field.get(bean);
			if (param == null) {
				continue;
			} else if (param instanceof Integer) {// 判断变量的类型
				int value = ((Integer) param).intValue();
				dbObject.put(varName, value);
			} else if (param instanceof String) {
				String value = (String) param;
				dbObject.put(varName, value);
			} else if (param instanceof Double) {
				double value = ((Double) param).doubleValue();
				dbObject.put(varName, value);
			} else if (param instanceof Float) {
				float value = ((Float) param).floatValue();
				dbObject.put(varName, value);
			} else if (param instanceof Long) {
				long value = ((Long) param).longValue();
				dbObject.put(varName, value);
			} else if (param instanceof Boolean) {
				boolean value = ((Boolean) param).booleanValue();
				dbObject.put(varName, value);
			} else if (param instanceof Date) {
				Date value = (Date) param;
				dbObject.put(varName, value);
			} else if (param instanceof List) {
				List<Object> list = (List<Object>) param;
				dbObject.put(varName, list);
			} else if (param instanceof Map) {
				Map<Object, Object> map = (Map<Object, Object>) param;
				dbObject.put(varName, map);
			}
			// 恢复访问控制权限
			field.setAccessible(accessFlag);
		}
		return dbObject;
	}
}
